package com.dmagic.cms.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.lang3.SystemUtils;


/**
 * <h3>Class name</h3> <h4>Description</h4> <h4>Special Notes</h4>
 * 
 * @ver 0.1
 * @author songlin.li 2012-09-18
 */
public class SystemUtil extends SystemUtils {
    /**
     * @description : get windows address info
     * 
     * @author songlin.li
     * @return
     */
    public static Map<String, String> getSystemAddressInfo() {
        Map<String, String> m = new HashMap<String, String>();
        String os = System.getProperty("os.name");
        if (os != null && os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;

                while ((line = br.readLine()) != null) {
                    if (line.indexOf("IP Address") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        String address = line.substring(index);
                        m.put("IP Address", address.trim());
                    }
                    if (line.indexOf("Host Name") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        String hostName = line.substring(index);
                        m.put("Host Name", hostName.trim());
                    }
                }
                br.close();
            } catch (IOException e) {
            }
        }
        return m;
    }

    /**
     * @description : get SystemInfo
     * @return
     */
    public static List<Entry<Object, Object>> getSystemInfo() {
        Properties props = System.getProperties();
        List<Entry<Object, Object>> l = new ArrayList<Entry<Object, Object>>();
        for (Map.Entry<Object, Object> s : props.entrySet()) {
            l.add(s);
        }
        return l;
    }
}